package me.bay.gt.java;

import java.sql.*;
import java.util.List;

public class JDBC2Exercise {
    private final String dbUrl;
    private final String dbUser;
    private final String dbPassword;

    public JDBC2Exercise(String dbUrl, String dbUser, String dbPassword) {
        this.dbUrl = dbUrl;
        this.dbUser = dbUser;
        this.dbPassword = dbPassword;
    }

    public void insert(Hero hero) throws SQLException {
        try (Connection conn = getConnection()) {
            String sql = "INSERT INTO heroes (name," +
                    " hp_max, hp_growth, hp_start," +
                    " mp_max, mp_growth, mp_start," +
                    " attack_max, attack_growth, attack_start," +
                    " defense_max, defense_growth, defense_start," +
                    " hp_5s_max, hp_5s_growth, hp_5s_start," +
                    " mp_5s_max, mp_5s_growth, mp_5s_start," +
                    " attack_speed_max, attack_range," +
                    " role_main, role_assist, birthdate) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                conn.setAutoCommit(false);

                ps.setString(1, hero.getName());
                ps.setFloat(2, hero.getHpMax());
                ps.setFloat(3, hero.getHpGrowth());
                ps.setFloat(4, hero.getHpStart());
                ps.setFloat(5, hero.getMpMax());
                ps.setFloat(6, hero.getMpGrowth());
                ps.setFloat(7, hero.getMpStart());
                ps.setFloat(8, hero.getAttackMax());
                ps.setFloat(9, hero.getAttackGrowth());
                ps.setFloat(10, hero.getAttackStart());
                ps.setFloat(11, hero.getDefenseMax());
                ps.setFloat(12, hero.getDefenseGrowth());
                ps.setFloat(13, hero.getDefenseStart());
                ps.setFloat(14, hero.getHp5sMax());
                ps.setFloat(15, hero.getHp5sGrowth());
                ps.setFloat(16, hero.getHp5sStart());
                ps.setFloat(17, hero.getMp5sMax());
                ps.setFloat(18, hero.getMp5sGrowth());
                ps.setFloat(19, hero.getMp5sStart());
                ps.setFloat(20, hero.getAttackSpeedMax());
                ps.setString(21, hero.getAttackRange());
                ps.setString(22, hero.getRoleMain());
                ps.setString(23, hero.getRoleAssist());
                ps.setDate(24, Date.valueOf(hero.getBirthdate()));

                ps.executeUpdate();
                conn.commit();
            }
        }
    }

    public void insert(List<Hero> heroes) throws SQLException {
        try (Connection conn = getConnection()) {
            String sql = "INSERT INTO heroes (name," +
                    " hp_max, hp_growth, hp_start," +
                    " mp_max, mp_growth, mp_start," +
                    " attack_max, attack_growth, attack_start," +
                    " defense_max, defense_growth, defense_start," +
                    " hp_5s_max, hp_5s_growth, hp_5s_start," +
                    " mp_5s_max, mp_5s_growth, mp_5s_start," +
                    " attack_speed_max, attack_range," +
                    " role_main, role_assist, birthdate) " +
                    "VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
            try (PreparedStatement ps = conn.prepareStatement(sql)) {
                conn.setAutoCommit(false);

                for (Hero hero : heroes) {
                    ps.setString(1, hero.getName());
                    ps.setFloat(2, hero.getHpMax());
                    ps.setFloat(3, hero.getHpGrowth());
                    ps.setFloat(4, hero.getHpStart());
                    ps.setFloat(5, hero.getMpMax());
                    ps.setFloat(6, hero.getMpGrowth());
                    ps.setFloat(7, hero.getMpStart());
                    ps.setFloat(8, hero.getAttackMax());
                    ps.setFloat(9, hero.getAttackGrowth());
                    ps.setFloat(10, hero.getAttackStart());
                    ps.setFloat(11, hero.getDefenseMax());
                    ps.setFloat(12, hero.getDefenseGrowth());
                    ps.setFloat(13, hero.getDefenseStart());
                    ps.setFloat(14, hero.getHp5sMax());
                    ps.setFloat(15, hero.getHp5sGrowth());
                    ps.setFloat(16, hero.getHp5sStart());
                    ps.setFloat(17, hero.getMp5sMax());
                    ps.setFloat(18, hero.getMp5sGrowth());
                    ps.setFloat(19, hero.getMp5sStart());
                    ps.setFloat(20, hero.getAttackSpeedMax());
                    ps.setString(21, hero.getAttackRange());
                    ps.setString(22, hero.getRoleMain());
                    ps.setString(23, hero.getRoleAssist());
                    ps.setDate(24, Date.valueOf(hero.getBirthdate()));
                    ps.addBatch();
                }

                ps.executeBatch();
                conn.commit();
            }
        }
    }

    private Connection getConnection() throws SQLException {
        return DriverManager.getConnection(dbUrl, dbUser, dbPassword);
    }
}
